<?php

namespace app\common\logic;

require_once EXTEND_PATH . 'aliyun/aliyun-php-sdk-oss/autoload.php';

use OSS\Core\OssException;
use OSS\OssClient;

class OssLogic extends Logic
{
    /**
     *  上传oss
     * @param $bucket
     * @param $endPoint
     * @param $src_file
     * @param $dst_file
     * @return array|null
     */
    public static function oss_file_upload($endPoint, $bucket, $src_file, $dst_file)
    {
        //实例化OSS
        $oss = new OssClient(config('aliyun.AccessKeyId'), config('aliyun.AccessKeySecret'), $endPoint);
        try {
            //uploadFile的上传方法
            $data = $oss->uploadFile($bucket, $dst_file, $src_file);

            if (is_array($data) && isset($data['info'])) {
                $arr = config('aliyun.convert_url');
                $dst_url = $data['info']['url'];
                foreach ($arr as $k => $v) {
                    $dst_url = str_replace($k, $v, $dst_url);
                }

                if ($data['info']['http_code'] == 200) {
                    return ['code' => 1, 'msg' => 'success', 'data' => $dst_url];
                }
            }
            return ['code' => 0, 'msg' => $data];
        } catch (OssException $e) {
            log_print($e->getErrorMessage());
            return ['code' => 0, 'msg' => $e->getMessage()];
        }

    }
}